#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
core.py

Created by Mikio Hokari on 2009-07-20.
Copyright (c) 2009 CMScom. All rights reserved.
"""
from config import rx_all, KANA_MIN, KANA_MAX, KANA_DIF, \
        pattern, pattern_g

def bigrams(xs, isglob):
    lxs = list(xs)
    for x in lxs:
        for y in bigrams_(x, isglob):
            yield y

def findallg(rx, s):
    return [g.group() for g in rx.finditer(s)]

def bigrams_(s, isglob):
    assert isinstance(s, unicode)
    return bisplitx(findallg((isglob and pattern_g or pattern), s) , isglob)

def bisplitx(xss, isglob):
    # print xss, isglob
    for xs in xss:
        if not xs:
            break
        if not rx_all.match(xs[0]):
            yield xs
        else:
            for x in bisplit(xs, isglob):
                # yield kananormalize(x)
                yield x

def bisplit(xs, isglob):
    assert xs
    length = len(xs)
    if isglob:
        split_length = length
    elif length == 1:
        yield xs
        return
    else:
        split_length = length # - 1
    for i in xrange(split_length):
        w = xs[i:i+2]
        #if len(w) == 1 and isglob and i == split_length - 1:
        if len(w) == 1 and isglob and i == split_length - 1 == 0:
            if w not in u"*?":
                yield w + "*"
        else:
            yield w

def kananormalize(xs):
    assert isinstance(xs, unicode)
    return u"".join(
        ## unichr(ord(x) + KANA_DIF) if KANA_MIN <= ord(x) <= KANA_MAX else x
        (KANA_MIN <= ord(x) <= KANA_MAX) and unichr(ord(x) + KANA_DIF) or x
        for x in xs)
